package com.sptpc.andoidclass1015;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final List <String> data = new ArrayList<>();
        final String str [] = new String[]{"中国","美国","英国","日本","澳大利亚"};
        for (int i = 0 ; i<str.length ; ++i){
            data.add(str[i]);
        }

        setContentView(R.layout.activity_main);
        ListView lv_1 = findViewById(R.id.lv_1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,data);
        lv_1.setAdapter(adapter);
        lv_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this,"你点击了第"+(i+1)+"条数据",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
